home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / VGATUT2.ZIP / VGACIRC.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-08-11  |  7.5 KB  |  363 lines

  1. {$X+}
  2. {$G+}
  3.  
  4.  
  5. program Cricle(input, output);
  6.  
  7. Uses Crt;
  8.  
  9. Const VGA = $A000;
  10.  
  11. {------------------------------------------------------------------------}
  12.  
  13. procedure Set_Vid_mode_to_320x200;
  14.  
  15. Begin
  16.     asm
  17.        mov ax, 13h      {store 13h in AX}
  18.        int 10h          {call interrupt }
  19.     end;
  20.  
  21. end;
  22.  
  23. {------------------------------------------------------------------------}
  24.  
  25. Procedure Cls (Colour : Byte);
  26.    { This clears the screen to the specified color }
  27. BEGIN
  28.   Fillchar (Mem [$a000:0],64000,colour);
  29. END;
  30.  
  31. {------------------------------------------------------------------------}
  32.  
  33. procedure Print_ASM_Pixel (X, Y : Integer; Colour : Byte);
  34.  
  35. {This is our super fast Pixel algorithum}
  36.  
  37. begin
  38.     asm
  39.       mov ax, 0a000h   { point AX to video memory   }
  40.       mov es, ax       { move segment pointer to ES }
  41.                        { (actual pointer)           }
  42.       mov bx, [Y]
  43.       mov ax, bx       { register to register is faster by 1 clock}
  44.  
  45.       mov ah, al       { ax=y*256 + y}
  46.       mov al,  0       { ax=y*256    }
  47.  
  48.       shl bx, 6        { bx=y*64     }
  49.       add bx, ax       { bx=y*320    }
  50.  
  51.       add bx, [X]      { ax=(y*320)+x}
  52.       mov di, bx       { move video pointer to correct place}
  53.  
  54.       mov al, [Colour]
  55.       mov es:[di], al  { move colour to memory }
  56.    end;
  57. end;
  58.  
  59.  
  60.  
  61. {------------------------------------------------------------------------}
  62.  
  63. procedure Return_Vid_Mode_To_Text;
  64.  
  65. begin
  66.    asm
  67.     mov ax, 03h      {store 03h in AX}
  68.     int 10h          {call interrupt }
  69.    end;
  70. end;
  71.  
  72. {------------------------------------------------------------------------}
  73.  
  74. Procedure DrawCircle( x, y, Radius : integer; Colour : byte);
  75.  
  76. var
  77.     Temp : real;
  78.     counter : integer;
  79.  
  80. begin
  81.     Temp:= 0;
  82.      repeat
  83.  
  84.            x := Round(Radius * cos(Temp));
  85.            y := Round(Radius * sin(Temp));
  86.  
  87.            Print_ASM_Pixel ( x + 160, y + 100, Colour );
  88.             Temp := Temp + 0.005;
  89.  
  90.        until (Temp > 6.3) {360 degrees = 6.3 rads}
  91. end;
  92.  
  93. {------------------------------------------------------------------------}
  94.  
  95. Procedure ImpDrawCircle( x, y, Radius : integer; Colour : byte);
  96.  
  97. var xt,
  98.     yt,
  99.     rt,
  100.     temp,
  101.     increment,
  102.     Counter : real;
  103.  
  104.     NewX,
  105.     NewY,
  106.     NewX1,
  107.     NewY1,
  108.  
  109.     NewX2,
  110.     NewY2,
  111.  
  112.     NewX3,
  113.     NewY3,
  114.  
  115.     NewX4,
  116.     NewY4 : integer;
  117.  
  118. begin
  119.  
  120.      if (Radius <= 0) then BEGIN
  121.        Radius := 1;
  122.      END;
  123.  
  124.  
  125.      increment := 1/Radius;
  126.  
  127.      {calculate X, Y change for each segment based on radius}
  128.  
  129.  
  130.       repeat
  131.  
  132.             xt := (Radius * cos(counter));
  133.             x := Round(xt);
  134.             yt := (Radius * sin(counter));
  135.             y := Round(yt);
  136.  
  137.  
  138.             If (abs ((xt - x)) < 0.5 ) then BEGIN
  139.  
  140.                       if (xt > 0) then BEGIN
  141.                          NewX := (x + 1);
  142.  
  143.                       END
  144.  
  145.                   else
  146.  
  147.                       BEGIN
  148.                         NewX :=  (x - 1);
  149.                       END;
  150.                END
  151.  
  152.             else
  153.  
  154.                    BEGIN
  155.                      NewX := x;
  156.  
  157.            END;
  158.  
  159.  
  160.       if (  abs(yt - y) < 0.5) then BEGIN
  161.  
  162.                     if (yt > 0) then BEGIN
  163.                       NewY := (y + 1);
  164.  
  165.                     END
  166.  
  167.                   else
  168.  
  169.                      BEGIN
  170.  
  171.                       NewY := (y - 1);
  172.  
  173.                     END;
  174.           END
  175.       else
  176.  
  177.                BEGIN
  178.                     NewY := Round(y);
  179.  
  180.                     NewX1 := NewX + X;
  181.                     NewY1 := NewY + Y;
  182.  
  183.                     NewX2 := (NewX * -1) + X;
  184.                     NewY2 := (NewY * -1) + Y;
  185.  
  186.                     NewX3 := (NewX * -1) + X;
  187.                     NewY3 :=  NewY + Y;
  188.  
  189.                     NewX4 :=  NewX + X;
  190.                     NewY4 :=  (NewY * -1) + Y;
  191.  
  192.       Mem [VGA:(NewY1 * 256) + (NewY1 * 64) + NewX1] := Colour;
  193.       Mem [VGA:(NewY2 * 256) + (NewY2 * 64) + NewX2] := Colour;
  194.       Mem [VGA:(NewY3 * 256) + (NewY3 * 64) + NewX3] := Colour;
  195.       Mem [VGA:(NewY4 * 256) + (NewY4 * 64) + NewX4] := Colour;
  196.  
  197.        increment := (increment + increment);
  198.             END;
  199.      until (increment > 6.4);
  200.    Mem [VGA:(NewY4 * 256) + (NewY4 * 64) + NewX4] := Colour;
  201.  
  202. end;
  203.  
  204. {------------------------------------------------------------------------}
  205.  
  206.  
  207. Procedure SmallPoorCircle;
  208.  
  209. begin
  210.     DrawCircle( 160, 100, 2, 15);
  211.  
  212.     ReadKey;
  213. end;
  214.  
  215. {------------------------------------------------------------------------}
  216.  
  217. Procedure SmallImprCircle;
  218.  
  219. begin
  220.  
  221.     ImpDrawCircle( 160, 100, 2, 15);
  222.     ReadKey;
  223. end;
  224.  
  225. {------------------------------------------------------------------------}
  226.  
  227. procedure PoorCocen;
  228.  
  229. var Temp : integer;
  230.  
  231. begin
  232.  
  233. Temp := 0;
  234.     repeat
  235.      DrawCircle( 160, 100, Temp, Temp );
  236.      Temp := Temp + 3;
  237.     until (Temp > 100);
  238.  
  239.     ReadKey;
  240.  
  241. end;
  242.  
  243.  
  244. {------------------------------------------------------------------------}
  245.  
  246. procedure ImprCocen;
  247.  
  248. var Temp : integer;
  249.  
  250. begin
  251.  
  252. Temp := 0;
  253.     repeat
  254.      ImpDrawCircle( 160, 100, Temp, Temp );
  255.      Temp := Temp + 3;
  256.     until (Temp > 100);
  257.  
  258.     ReadKey;
  259.  
  260. end;
  261.  
  262. {------------------------------------------------------------------------}
  263. procedure PoorRandCirc;
  264.  
  265.  var Temp : integer;
  266.  
  267. begin
  268.  
  269.     for temp := 1 to 50 do BEGIN
  270.     DrawCircle( Random(320), Random(200), Random(100), Random(256) );
  271.     END;
  272.   ReadKey;
  273. end;
  274.  
  275. {------------------------------------------------------------------------}
  276.  
  277. procedure ImprRandCirc;
  278.  
  279.  var Temp : integer;
  280.  
  281. begin
  282.  
  283.     for temp := 1 to 100 do
  284.     ImpDrawCircle( Random(320), Random(200), Random(100), Random(256) );
  285.  
  286.   ReadKey;
  287. end;
  288.  
  289. {------------------------------------------------------------------------}
  290. procedure Intro;
  291.  
  292. begin
  293.  
  294.     ClrScr;
  295.  
  296.     WriteLn ('Hi there & welcome to the second part of this VGA tutorial.' );
  297.     WriteLn ('This program concerns its self with circles.  We look at two functions');
  298.     WriteLn ('for drawing them....');
  299.     WriteLn ;
  300.     WriteLn ('1.   This routine is slow & inacurate but forms the basis of the second' );
  301.     WriteLn ('     routine.');
  302.     WriteLn ;
  303.     WriteLn ('2.   Much better.  Faster & more accurate.' );
  304.     WriteLn ;
  305.     WriteLn ('Take a look.....' );
  306.  
  307.     ReadKey;
  308.  
  309. end;
  310.  
  311.  
  312. {------------------------------------------------------------------------}
  313.  
  314. procedure Outro;
  315.  
  316. begin
  317.  
  318.     WriteLn( 'Good.  Now we have a semi-decent circle routine to add to our library.');
  319.     WriteLn( 'I hope youve enjoyed this tutorial and that youll find it useful.');
  320.     WriteLn( 'Many thanks to Richard Griffiths whos been porting this code to Pascal');
  321.     WriteLn( 'for me.');
  322.     WriteLn;
  323.     WriteLn( 'As yet, this tutorial is still not available by FTP but Im working');
  324.     WriteLn( 'on it.Bye for now......  ' );
  325.     WriteLn;
  326.     WriteLn( 'Barny Mercer      : barny.mercer@zetnet.co.uk ' );
  327.     WriteLn( '                  : http://www.zetnet.co.uk/users/bmercer/ ');
  328.     WriteLn;
  329.     WriteLn( 'Richard Griffiths : richard.griffiths@zetnet.co.uk ' );
  330.     WriteLn( '                  : http://www.zetnet.co.uk/users/rgriff/');
  331.  
  332.     ReadKey;
  333.  
  334. end;
  335.  
  336. {------------------------------------------------------------------------}
  337.  
  338.  
  339. begin {the Main program}
  340.  
  341.      Intro;
  342.  
  343.      Set_Vid_mode_to_320x200;
  344.  
  345.      Cls (1);
  346.      SmallPoorCircle;
  347.      Cls (1);
  348.      SmallImprCircle;
  349.      Cls (1);
  350.      PoorCocen;
  351.      Cls (1);
  352.      ImprCocen;
  353.      Cls (1);
  354.      PoorRandCirc;
  355.      Cls (1);
  356.      ImprRandCirc;
  357.      Cls (1);
  358.  
  359.      Return_Vid_Mode_To_Text;
  360.  
  361.      Outro;
  362. end.
  363.